package cn.iocoder.yudao.module.mini.service.project;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.mini.controller.admin.project.vo.ProjectCreateReqVO;
import cn.iocoder.yudao.module.mini.controller.admin.project.vo.ProjectExportReqVO;
import cn.iocoder.yudao.module.mini.controller.admin.project.vo.ProjectPageReqVO;
import cn.iocoder.yudao.module.mini.controller.admin.project.vo.ProjectUpdateReqVO;
import cn.iocoder.yudao.module.mini.convert.project.ProjectConvert;
import cn.iocoder.yudao.module.mini.dal.dataobject.project.ProjectDO;
import cn.iocoder.yudao.module.mini.dal.mysql.project.ProjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mini.enums.ErrorCodeConstants.PROJECT_NOT_EXISTS;

/**
 * 服务项目 Service 实现类
 *
 * @author dzy
 */
@Service
@Validated
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;

    @Override
    public Long createProject(ProjectCreateReqVO createReqVO) {
        // 插入
        ProjectDO project = ProjectConvert.INSTANCE.convert(createReqVO);
        project.setAreaCode(String.join(",", createReqVO.getAreaCodeList()));
        projectMapper.insert(project);
        // 返回
        return project.getId();
    }

    @Override
    public void updateProject(ProjectUpdateReqVO updateReqVO) {
        // 校验存在
        validateProjectExists(updateReqVO.getId());
        // 更新
        ProjectDO updateObj = ProjectConvert.INSTANCE.convert(updateReqVO);
        updateObj.setAreaCode(String.join(",",updateReqVO.getAreaCodeList()));
        projectMapper.updateById(updateObj);
    }

    @Override
    public void deleteProject(Long id) {
        // 校验存在
        validateProjectExists(id);
        // 删除
        projectMapper.deleteById(id);
    }

    private void validateProjectExists(Long id) {
        if (projectMapper.selectById(id) == null) {
            throw exception(PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public ProjectDO getProject(Long id) {
        ProjectDO projectDO = projectMapper.selectById(id);
        List<Long> AreaCodeList = Arrays.stream(projectDO.getAreaCode().split(",")).map(Long::parseLong).collect(Collectors.toList());
        projectDO.setAreaCodeList(AreaCodeList);
        return projectDO;
    }

    @Override
    public List<ProjectDO> getProjectList(Collection<Long> ids) {
        return projectMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProjectDO> getProjectPage(ProjectPageReqVO pageReqVO) {
        return projectMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProjectDO> getProjectList(ProjectExportReqVO exportReqVO) {
        return projectMapper.selectList(exportReqVO);
    }

}
